package com.ysjweb.dish.Controller;

import com.ysjweb.dish.Service.impl.UserServiceImpl;
import com.ysjweb.dish.pojo.Result;
import com.ysjweb.dish.tool.JwtTool;
import jakarta.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ysjweb.dish.pojo.DishUser;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RestController
public class userController {
    private String appId="a";//id
    private String secret="s";//

    @Autowired
    private UserServiceImpl UserService;


    @PutMapping("/favorite")
    public Result ChangeFavorite(Integer id,String favorite) {
        UserService.UpdateFavoriteById(id,favorite);
        return Result.success();
    }
    @GetMapping("/favorite")
    public Result GetFavorite(Integer id) {
        return Result.success(UserService.GetFavoriteById(id));
    }
    @PostMapping("/register/web")
    public Result Register(HttpServletResponse response,@RequestBody DishUser NewUser ) {
        if(UserService.AddUser(NewUser.getUsername(), NewUser.getPassword())){
            Map<String,Object> map = new HashMap<>();
            map.put("id",NewUser.getId());
            map.put("username",NewUser.getUsername());
            map.put("password",NewUser.getPassword());
            String token= JwtTool.generateJwt(map);
            response.addHeader("token",token);
            return Result.success(token);//注册完成后返回jwt//这样不需要再一次调用login接口
        }
        return Result.error("Register failed");
    }
    @PostMapping("/login/web")
    public Result Login(HttpServletResponse response, @RequestBody DishUser user) {
        if(UserService.ExistUsername(user.getUsername(), user.getPassword())){
            Map<String,Object> map = new HashMap<>();
            map.put("id",UserService.GetUserId(user.getUsername(), user.getPassword()));
            map.put("username",user.getUsername());
            String jwt= JwtTool.generateJwt(map);
            response.addHeader("token",jwt);
            return Result.success(jwt);
        }else{
            return Result.error("NO_LOGIN");
        }
    }

    @PostMapping("/login/wechat")
    public Result LoginWechat(@RequestParam String code) {
        //先通过code获得openid和session_key再构建jwt令牌
        String url= "https://api.weixin.qq.com/sns/jscode2session" +
                "?appid=" + appId +
                "&secret=" + secret +
                "&js_code=" + code +
                "&grant_type=authorization_code";
        RestTemplate restTemplate = new RestTemplate();
        String response=restTemplate.getForObject(url, String.class);
        JSONObject json=new JSONObject(response);//反序列化
        String openId= json.getString("openId");
        String session_key= json.getString("session_key");
        if(openId==null){
            return Result.error("微信登录失败");
        }

        Map<String,Object> map = new HashMap<>();
        map.put("openId",openId);
        map.put("session_key",session_key);
        String Jwt=JwtTool.generateJwt(map);
        return Result.success(Jwt);
    }
}
